package com.example.yzx.allriddle;

import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.yzx.allriddle.utils.Db;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class StarDetailActivity extends AppCompatActivity {
    private int pageid=0;
    private String type=null;
    private TextView context;
    private int deleteid;
    private Db db = new Db(this, 1);
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_star_detail);
        //收藏引导
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        btn=findViewById(R.id.star);
        context=findViewById(R.id.ct);
        Bundle bundle = this.getIntent().getExtras();
        if(bundle!=null&&bundle.getInt("id")!=0){
            pageid=bundle.getInt("id");
            type=bundle.getString("type");
            deleteid=bundle.getInt("did");
        }
        System.out.println(pageid+"type"+type);
        initial();

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void unstar(View view){
        SQLiteDatabase sqLiteDb = db.getWritableDatabase();
        sqLiteDb.delete("star", "id="+deleteid+"", null);
        sqLiteDb.close();
        AlertDialog alertDialog2 = new AlertDialog.Builder(this)
                .setTitle("取消收藏成功")//标题
                .setMessage(" ")//内容
                .create();
        alertDialog2.show();
    }
    private Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String title = (String) msg.obj;
                context.setText(title);
            }
        }

    };

    private void   initial(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(type+pageid+"查看数据");
                try {
                    OkHttpClient client=new OkHttpClient();
                    String url="";
                    if(type.equals("xiaohua/text"))
                        url="https://api.jisuapi.com/"+type+"?pagenum="+pageid+"&pagesize=1&sort=addtime&appkey=85cdfb160033ed3f";
                    else if(type.equals("miyu/search"))
                        url="https://api.jisuapi.com/"+type+"?pagenum="+pageid+"&pagesize=1&appkey=85cdfb160033ed3f";
                    else if(type.equals("jzw/search"))
                        url="https://api.jisuapi.com/"+type+"?keyword=&pagenum="+pageid+"&pagesize=1&appkey=85cdfb160033ed3f";
                    Request request=new Request.Builder()
                            .url(url)
                            .build();
                    Response response=client.newCall(request).execute();
                    String data=response.body().string();
                    json(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void json(String data){
        try {
            JSONObject json = new JSONObject(data);
            JSONObject result = json.getJSONObject("result");
            String pid="";
            JSONArray results = result.getJSONArray("list");
            JSONObject obj=results.getJSONObject(0);
            String content=obj.getString("content");
            if(!type.equals("xiaohua/text")){
                content+="\n\n         谜底 ："+obj.getString("answer");
            }
            //传送
            Message message = hand.obtainMessage();
            message.what = 1;
            message.obj = content;
            System.out.println(content+"内容");
            hand.sendMessage(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
